package com.duo.controller.admin;

import com.duo.dto.EmployeeDTO;
import com.duo.dto.EmployeeLoginDTO;
import com.duo.dto.EmployeePageQueryDTO;
import com.duo.entity.Employee;
import com.duo.result.BaseResult;
import com.duo.result.PageResult;
import com.duo.service.EmployeeService;
import com.duo.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class
EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private RedisUtils redisUtils;



    /**
     * 登录
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Object login(HttpServletRequest request, @RequestBody @Valid EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        String token = request.getHeader("token");
        return employeeService.login(employeeLoginDTO, token);
    }

    /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @PostMapping("/save")
    public Object save(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工：{}",employeeDTO);
        employeeService.save(employeeDTO);
        return BaseResult.success("新增员工成功");
    }

    /**
     * 员工分页查询
     * @param employeePageQueryDTO
     * @return
     */
    @PostMapping("/page")
    public Object page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询，参数为：{}", employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return BaseResult.success("查询成功", pageResult);
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Object getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return BaseResult.success("查询成功", employee);
    }

    /**
     * 修改员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping("/update")
    public Object update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息：{}", employeeDTO);
        employeeService.update(employeeDTO);
        return BaseResult.success("修改成功");
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    public Object logout(@RequestParam String token) {
        redisUtils.deleteObject(String.format("%s", token));
        return BaseResult.success("退出成功");
    }



}
